package com.wolfalpha.utron.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wolfalpha.utron.R;
import com.wolfalpha.utron.bean.Teamer;
import com.wolfalpha.utron.view.CircleImageView;

import java.util.List;

/**
 * 组成员列表适配器
 * <p/>
 * Created by luodon on 15/9/12.
 */
public class ItemTeamerAdapter extends BaseAdapter {


    private LayoutInflater inflater;
    private Context context;
    private List<Teamer> teamers;

    public ItemTeamerAdapter(Context context, List<Teamer> rs) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        teamers = rs;
    }

    public void setData(List<Teamer> rs) {
        if (rs != null) {
            teamers = rs;
            this.notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return teamers.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_user_base, null);
            viewHolder = new ViewHolder();
            viewHolder.tvName = (TextView) convertView.findViewById(R.id.item_tv_user_base_user_name);
            viewHolder.tvTime = (TextView) convertView.findViewById(R.id.item_tv_user_base_time);
            viewHolder.tvSign = (TextView) convertView.findViewById(R.id.item_tv_user_base_user_sign);
            viewHolder.cvHead = (CircleImageView) convertView.findViewById(R.id.item_civ_user_base_user_head);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        //设置控件内容
        viewHolder.tvName.setText(teamers.get(position).getUser_name());
        viewHolder.tvSign.setText(teamers.get(position).getUser_sign());
        viewHolder.tvTime.setText(teamers.get(position).getRole());
        Picasso.with(context)
                .load(teamers.get(position).getUser_head())
                .placeholder(R.mipmap.head_default)
                .into(viewHolder.cvHead);
        final int p = position;
        viewHolder.cvHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mlistenr != null) {
                    mlistenr.clickHead(teamers.get(p).getUser_id());
                }
            }
        });

        return convertView;
    }

    /**
     * 控件
     */
    class ViewHolder {
        private TextView tvName;
        private TextView tvTime;
        private TextView tvSign;
        private CircleImageView cvHead;
    }

    private OnHeadClickListener mlistenr;

    public void setOnHeadClickListener(OnHeadClickListener ml) {
        mlistenr = ml;
    }

    public interface OnHeadClickListener {
        public void clickHead(int id);
    }
}
